<?php
namespace app\controllers;

use Yii;
use app\common\ErrorCode;
use app\controllers\BaseController;
use app\models\GameUser;
use app\models\Morning;
use app\models\MorningRecord;

class RankController extends BaseController
{
    public $enableCsrfValidation = false;

	public function actionScorerank(){
		if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
			$session = $postData['session'];
    		$key = '_3rd_session:'.$session;
    	    $redisinfo = Yii::$app->redis->get($key);
    	    $openid = $this->RedisExplode($redisinfo);
            $userdata = $this->judgeSession($session);
            #获取userdata userid
            if( !empty($userdata['code']) ){
                BaseController::sendClientId($client_id, $data);
                exit(0);
            }
            $userid = $userdata['id'];
    		//获取个人的积分排行
    		$user = $this->getUserscore($userdata);
    	    //获取前十积分排行
    	    $scoreRank =  Morning::ToptenbyScore();
            #
    	    $scoredata = array(
                'open' => true,
    	    	'user' => $user,
    	    	'scoreRank' => $scoreRank,
    	    );
            $data = array(
                'code' => ErrorCode::$SUCCESS,
                'code_info' => '成功',
                'data' => $scoredata, 
            );
    		BaseController::sendMessage($userid, json_encode($data));
		}else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
	}


	public function actionMorningrank(){
		if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
			$session = $postData['session'];
            #获取userdata userid
    	    $userdata = $this->judgeSession($session);
            if( !empty($userdata['code']) ){
                BaseController::sendClientId($client_id, $data);
                exit(0);
            }
            $userid = $userdata['id'];
            #
    		//获取个人的今天起床排行
    	    $user = $this->getUsermorning($userdata);
            //获取前十起床的排行
            $morningRank = Morning::TopTenbydatatime();
            $morningdata = array(
                'open' => true,
            	'user' => $user,
            	'morningRank' => $morningRank,
            );
    		$data = array(
                'code' => ErrorCode::$SUCCESS,
                'code_info' => '成功',
                'data' => $morningdata, 
            );
            BaseController::sendMessage($userid, json_encode($data));
		}else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
	}


	public function getUserscore($userdata){
		$userid = $userdata['id'];
		$nickname = $userdata['nickname'];
        $avatarurl = $userdata['avatarUrl'];
        $userdata = Morning::data_by_id($userid);
        $userScore = $userdata['totalscore'];
        if($userScore == 0){
            $user = array(
                'sign' => false,
                'content' => '你还没开始签到获得积分'
            );
        }else{
            $userRank = Morning::getTotalScoreRank($userScore,$userdata['time'],$userdata['msectime']);
            $userdata = array(
                'ScoreRank' => $userRank,
                'nickname' => $nickname,
                'avatarurl' => $avatarurl,
                'totalscore' => $userScore
            );
            $user = array(
                'sign' => true,
                'user' => $userdata,
            );
        }
        return $user;
	}


	public function getUsermorning($userdata){
		$userid = $userdata['id'];
		$nickname = $userdata['nickname'];
        $avatarurl = $userdata['avatarUrl'];
        $morningTime = Morning::getMorningTime($userid);
        if( $morningTime == -1 ){
            $user = array(
                'signtoday' => false,
                'content' => '你今天还没开始签到'
                );
        }else{
        	$morningRank = Morning::getMorningRank($morningTime['time'], $morningTime['msectime']);
            $msectime = $morningTime['msectime']/1000000;
            $msectime = round($msectime,3);
            $msectime = sprintf("%01.3f",$msectime);
            list($usec, $msectime) = explode(".",$msectime);
            $msectime = str_pad($msectime,3,"0",STR_PAD_RIGHT);
            $datetime = explode(' ', $morningTime['time']);
            $time = $datetime[1]. '.' . $msectime;
        	$userdata = array(
        		'rank' => $morningRank,
        		'nickname' => $nickname,
        		'avatarurl' => $avatarurl,
                'time' => $time,
        	);
            $user = array(
                'signtoday' => true,
                'user' => $userdata 
            );
        }
        return $user;
	}

}
